package com.qys.livesMall.navy.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qys.livesMall.common.exception.QysException;
import com.qys.livesMall.framework.utils.SecurityUtils;
import com.qys.livesMall.navy.enums.NavyMessageEnum;
import com.qys.livesMall.navy.mapper.H2NavyLanguageManageMapper;
import com.qys.livesMall.navy.model.dto.NavyLanguageManageDto;
import com.qys.livesMall.navy.model.dto.NavyLanguageManageQueryPackageDto;
import com.qys.livesMall.navy.model.entity.H2NavyLanguageManage;
import com.qys.livesMall.navy.model.vo.NavyLanguageManageVo;
import com.qys.livesMall.navy.service.H2NavyLanguageManageService;
import jodd.util.StringUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * <p>
 * 水军语言管理 服务实现类
 * </p>
 *
 * @author wzy
 * @since 2023-07-04
 */
@Service
public class H2NavyLanguageManageServiceImpl extends ServiceImpl<H2NavyLanguageManageMapper, H2NavyLanguageManage> implements H2NavyLanguageManageService {
    /**
     * 新增语言管理
     * @param navyLanguageManageDto
     * @return
     */
    @Override
    public Boolean add(NavyLanguageManageDto navyLanguageManageDto) {
        H2NavyLanguageManage h2NavyLanguageManage=new H2NavyLanguageManage();
        BeanUtil.copyProperties(navyLanguageManageDto,h2NavyLanguageManage);
        h2NavyLanguageManage.setCreateBy(SecurityUtils.getUsername());
        h2NavyLanguageManage.setCreateTime(LocalDateTime.now());
        return save(h2NavyLanguageManage);
    }

    /**
     * 修改语言管理
     * @param navyLanguageManageDto
     * @return
     */
    @Override
    public Boolean update(NavyLanguageManageDto navyLanguageManageDto) {
        H2NavyLanguageManage h2NavyLanguageManage=getById(navyLanguageManageDto.getId());
        if (null==h2NavyLanguageManage){
            throw new QysException(NavyMessageEnum.NAVY_NOT_ID);
        }
        h2NavyLanguageManage.setName(navyLanguageManageDto.getName());
        h2NavyLanguageManage.setSerialNumber(navyLanguageManageDto.getSerialNumber());
        h2NavyLanguageManage.setContent(navyLanguageManageDto.getContent());
        h2NavyLanguageManage.setUpdateBy(SecurityUtils.getUsername());
        h2NavyLanguageManage.setUpdateTime(LocalDateTime.now());
        return updateById(h2NavyLanguageManage);
    }

    /**
     * 查询语言管理
     * @param dto
     * @return
     */
    @Override
    public IPage<NavyLanguageManageVo> query(NavyLanguageManageQueryPackageDto dto) {
        LambdaQueryWrapper<H2NavyLanguageManage> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(H2NavyLanguageManage::getDeleted, 0);
        queryWrapper.eq(H2NavyLanguageManage::getType, dto.getType());
        queryWrapper.and(StringUtil.isNotBlank(dto.getKeywords()),wrapper->{
            wrapper.like(H2NavyLanguageManage::getSerialNumber, dto.getKeywords());
            wrapper.or().like(H2NavyLanguageManage::getContent, dto.getKeywords());
            wrapper.or().like(H2NavyLanguageManage::getName, dto.getKeywords());
        });
        queryWrapper.orderByAsc(H2NavyLanguageManage::getSerialNumber);
        IPage<NavyLanguageManageVo> page=page(new Page<>(dto.getPageNum(), dto.getPageSize()), queryWrapper).convert(e -> {
            NavyLanguageManageVo navyLanguageManageVo=new NavyLanguageManageVo();
            BeanUtils.copyProperties(e,navyLanguageManageVo);
            return navyLanguageManageVo;
        });
        return page;
    }

    @Override
    public NavyLanguageManageVo info(Long id) {
        H2NavyLanguageManage h2NavyLanguageManage=getById(id);
        if (null==h2NavyLanguageManage){
            throw new QysException(NavyMessageEnum.NAVY_NOT_ID);
        }
        NavyLanguageManageVo navyLanguageManageVo=new NavyLanguageManageVo();
        BeanUtils.copyProperties(h2NavyLanguageManage,navyLanguageManageVo);
        return navyLanguageManageVo;
    }
}
